import { DatabaseEnvStore } from "@/modules/dev-env/database/common/database-env-store.ts";
import type { LocalVersionItem, ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";
import path from "@/framework/util/path.ts";
import type { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import type { EnvTableAction } from "@/modules/dev-env/store/types.ts";
import { openInTerminal } from "@/util/system-utils2.ts";
import { MongodbHistoryMirror, MongodbLatestMirror } from "./mongodb-mirror.ts";
import MongodbSettings from "./mongodb-settings.vue";
import type { UnzipCompleteArgs } from "@common/archives-file-types.ts";
import { moveTempDir0ToVersionDir } from "@/modules/dev-env/common/install-utils.ts";

export class MongodbEnvStore extends DatabaseEnvStore<MongodbEnvStore> {
  constructor() {
    super("mongodb");
    this.icon = "icon-mongo";

    this.pageConfig = {
      showCurrentAction: false,
    };

    this.settingsComponent = MongodbSettings;
    this.tableActions = this._tableActions();
  }

  private _tableActions(): EnvTableAction[] {
    return [
      {
        key: "config-install",
        icon: "icon-setting",
        name: "配置/安装",
        onClick: version => (this.proxy.selectedItem = version),
      },
      {
        key: "open-in-shell",
        icon: "icon-terminal",
        name: "在终端打开",
        onClick: version => openInTerminal(version.installPath, { subPath: ["bin"] }),
      },
    ];
  }

  protected getMirrors0(): BaseEnvMirror[] {
    return [new MongodbLatestMirror(), new MongodbHistoryMirror()];
  }

  protected async getVersionInfo(versionDir: string): Promise<LocalVersionItem | undefined> {
    if (!(await native.fs.exists(path.join(versionDir, "bin/mongod.exe")))) {
      return undefined;
    }

    return { version: path.basename(versionDir), installPath: versionDir };
  }

  async afterUnzipComplete(e: UnzipCompleteArgs, version: ToolInstallVersionItem) {
    await moveTempDir0ToVersionDir(e.fileDir, this.installDir, version);
  }
}
